package leetcode;

import java.util.Arrays;

public class LeetCode322 {
    public static void main(String[] args) {
        LeetCode322 leetCode322 = new LeetCode322();
        leetCode322.coinChange(new int[]{1, 2, 5}, 11);
    }


    public int coinChange(int[] coins, int amount) {
        int length = coins.length;
        int[][] dp = new int[length + 1][amount + 1];
        for (int i = 0; i <= length; i++) {
            dp[i][0] = -1;
        }
        for (int i = 0; i <= amount; i++) {
            dp[0][i] = -1;
        }
        for (int i = 1; i <= length; i++) {
            for (int j = 1; j <= amount; j++) {
                int coin = coins[i - 1];
                if (j < coin)
                    dp[i][j] = dp[i - 1][j];
                else {
                    int k = j / coin;
                    int min = Integer.MAX_VALUE;
                    for (int l = 0; l < k; l++) {
                        int money = j - l * coin;
                        if (money == 0)
                            min = Math.min(l, min);
                        else if (dp[i - 1][money] != -1)
                            min = Math.min(dp[i - 1][money] + l, min);
                    }
                    dp[i][j] = min == Integer.MAX_VALUE ? -1 : min;
                }
            }
        }
        for (int i = 0; i <= length; i++) {
            System.out.println(Arrays.toString(dp[i]));

        }
        return dp[length][amount];
    }
//------------------------------------------------------------------
}
